using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Publicuse.Entity;
using Publicuse.Util;

namespace Publicuse.Export
{
  /// <summary>
  /// 日志信息辅助类
  /// </summary>
  public class LogExport
  {
    /// <summary>
    /// 登录日志数据设置
    /// </summary>
    /// <param name="excelName"></param>
    /// <param name="fileName"></param>
    /// <param name="list"></param>
    /// <param name="msg"></param>
    /// <returns></returns>
    public static MemoryStream ExportDataa(
      string fileName,
      string excelName,
      List<SigninInfo> list,
      out string msg
    )
    {
      msg = string.Empty;
      MemoryStream ms = ExcelUtil.GetExcelUtil.InitExportData(
        fileName,
        excelName,
        list,
        (row, cell, sheet1, TextSytle, srow, hssfworkbook) =>
        {
          foreach (SigninInfo item in list)
          {
            srow++;
            row = sheet1.CreateRow(srow);

            //登录账号
            cell = row.CreateCell(0);
            cell.CellStyle = TextSytle;
            cell.SetCellValue(item.logincode);

            //登录系统时间
            cell = row.CreateCell(1);
            cell.CellStyle = TextSytle;
            cell.SetCellValue(item.cTime.ToString("yyyy-MM-dd HH:mm:ss"));

            //IP地址
            cell = row.CreateCell(2);
            cell.CellStyle = TextSytle;
            cell.SetCellValue(item.ipaddress);

            //MAC地址
            cell = row.CreateCell(3);
            cell.CellStyle = TextSytle;
            cell.SetCellValue(item.lmacaddress);

            //操作系统名称
            cell = row.CreateCell(4);
            cell.CellStyle = TextSytle;
            cell.SetCellValue(item.osystem);

            //操作系统类型
            cell = row.CreateCell(5);
            cell.CellStyle = TextSytle;
            cell.SetCellValue(item.osystemtype);

            //浏览器信息
            cell = row.CreateCell(6);
            cell.CellStyle = TextSytle;
            cell.SetCellValue(item.browser);
          }
        },
        out msg,
        2
      );
      return ms;
    }

    /// <summary>
    /// 控制器日志数据设置
    /// </summary>
    /// <param name="excelName"></param>
    /// <param name="fileName"></param>
    /// <param name="list"></param>
    /// <param name="msg"></param>
    /// <returns></returns>
    public static MemoryStream ExportDatab(
      string fileName,
      string excelName,
      List<Controllerexelog> list,
      out string msg
    )
    {
      msg = string.Empty;
      MemoryStream ms = ExcelUtil.GetExcelUtil.InitExportData(
        fileName,
        excelName,
        list,
        (row, cell, sheet1, TextSytle, srow, hssfworkbook) =>
        {
          //设置列宽
          //sheet1.SetColumnWidth(1, 245 * 256);
          foreach (Controllerexelog item in list)
          {
            srow++;
            row = sheet1.CreateRow(srow);

            //操作账号
            cell = row.CreateCell(0);
            cell.CellStyle = TextSytle;
            cell.SetCellValue(item.ucode);

            //操作时间
            cell = row.CreateCell(1);
            cell.CellStyle = TextSytle;
            cell.SetCellValue(item.cTime.ToString("yyyy-MM-dd HH:mm:ss"));

            //IP地址
            cell = row.CreateCell(2);
            cell.CellStyle = TextSytle;
            cell.SetCellValue(item.ipaddress);

            //控制器名称
            cell = row.CreateCell(3);
            cell.CellStyle = TextSytle;
            cell.SetCellValue(item.name);

            //控制器方法
            cell = row.CreateCell(4);
            cell.CellStyle = TextSytle;
            cell.SetCellValue(item.method);

            //请求方式
            cell = row.CreateCell(5);
            cell.CellStyle = TextSytle;
            cell.SetCellValue(item.requestmode);

            //控制器参数
            cell = row.CreateCell(6);
            cell.CellStyle = TextSytle;
            cell.SetCellValue(item.parameterdata);

            ////控制器返回结果
            //cell = row.CreateCell(7);
            //cell.CellStyle = TextSytle;
            //cell.SetCellValue(item.backresult);

            //请求头信息
            cell = row.CreateCell(7);
            cell.CellStyle = TextSytle;
            cell.SetCellValue(item.hearddata);
          }
        },
        out msg,
        2
      );
      return ms;
    }

    /// <summary>
    /// 功能操作日志数据设置
    /// </summary>
    /// <param name="excelName"></param>
    /// <param name="fileName"></param>
    /// <param name="list"></param>
    /// <param name="msg"></param>
    /// <returns></returns>
    public static MemoryStream ExportDatac(
      string fileName,
      string excelName,
      List<Log> list,
      out string msg
    )
    {
      msg = string.Empty;
      MemoryStream ms = ExcelUtil.GetExcelUtil.InitExportData(
        fileName,
        excelName,
        list,
        (row, cell, sheet1, TextSytle, srow, hssfworkbook) =>
        {
          foreach (Log item in list)
          {
            srow++;
            row = sheet1.CreateRow(srow);

            //操作账号
            cell = row.CreateCell(0);
            cell.CellStyle = TextSytle;
            cell.SetCellValue(item.luser);

            //操作时间
            cell = row.CreateCell(1);
            cell.CellStyle = TextSytle;
            cell.SetCellValue(item.cTime.ToString("yyyy-MM-dd HH:mm:ss"));

            //IP地址
            cell = row.CreateCell(2);
            cell.CellStyle = TextSytle;
            cell.SetCellValue(item.ipaddress);

            //操作模块
            cell = row.CreateCell(3);
            cell.CellStyle = TextSytle;
            cell.SetCellValue(item.lmodule);

            //操作类型
            cell = row.CreateCell(4);
            cell.CellStyle = TextSytle;
            cell.SetCellValue(item.ltype);

            //操作角色
            cell = row.CreateCell(5);
            cell.CellStyle = TextSytle;
            cell.SetCellValue(item.lrole);

            //操作结果
            cell = row.CreateCell(6);
            cell.CellStyle = TextSytle;
            cell.SetCellValue(item.lsuccess == 1 ? "成功" : "失败");

            //操作内容
            cell = row.CreateCell(7);
            cell.CellStyle = TextSytle;
            cell.SetCellValue(item.lcontent);
          }
        },
        out msg,
        2
      );
      return ms;
    }
  }
}
